//
//  CustomNavigationController.m
//  iMore
//
//  Created by User on 28/08/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "CustomNavigationController.h"
#import <QuartzCore/QuartzCore.h>
#import "Utility.h"
#import "Config.h"
#define NAVIGATIONBAR_BACKGROUND_IMAGE_DEFAULT @"nav_bar.png"
 
@implementation CustomNavigationController

//@synthesize bgImageView;
- (id)initWithRootViewController:(UIViewController *)rootViewController {
	self = [super initWithRootViewController:rootViewController];
    if (self) {
        [self reloadNavigationUI:NAVIGATIONBAR_BACKGROUND_IMAGE_DEFAULT];
    }
    return self;
}

- (id)initWithRootViewController:(UIViewController *)rootViewController withBackgroundImage:(NSString*)image {
	self = [super initWithRootViewController:rootViewController];
    if (self) {
        if (image!=nil && ![image isEqualToString:@""]) {
            [self reloadNavigationUI:image];
        } else {            
            [self reloadNavigationUI:NAVIGATIONBAR_BACKGROUND_IMAGE_DEFAULT];
        }
    }
    return self;
}

- (void)reloadNavigationUI:(NSString *) barImage{
    UIImage *navigationImage = [Utility imageNamed:barImage];
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=5.0) {
        [self.navigationBar setBackgroundImage:navigationImage forBarMetrics:UIBarMetricsDefault];
    }else{
        [[self.navigationBar viewWithTag:NAVIGATIONBAR_TAG] removeFromSuperview];
        UIImageView *tempImageView = [[UIImageView alloc] initWithImage:navigationImage];
        tempImageView.frame=CGRectMake(0, 0, 320, 44);
        tempImageView.tag=NAVIGATIONBAR_TAG;
        self.bgImageView =tempImageView;
        tempImageView.layer.zPosition = -100;
        [self.navigationBar insertSubview:tempImageView atIndex:0];
       // [tempImageView release];
    }
    navigationImage =nil;
}


- (void) pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [super pushViewController:viewController animated:YES];
    if ([self.viewControllers count] > 1){
        [self backButton:viewController];
    }
    
}

- (void)backButton:(UIViewController *)viewController{
    if (viewController.navigationItem.leftBarButtonItem == nil) {
        UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:[Utility customButton:self action:@selector(backToPre) normalImage:@"navback_normal.png" lightedImage:@"navback_hover.png" frame:CGRectMake(0, 0, 58, 29) title:nil tag:10]];
        backBarButtonItem.style = UIBarButtonItemStylePlain;
        viewController.navigationItem.leftBarButtonItem = backBarButtonItem;
    }
}

- (void)backToPre {
    [self popViewControllerAnimated:YES];
}
// 在iOS6下已废弃
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return interfaceOrientation==UIInterfaceOrientationPortrait;
}


// Only on iOS6
- (BOOL)shouldAutorotate
{
    return NO;
}
// Only on iOS6
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

- (void)dealloc{
	//[bgImageView release];
	//bgImageView = nil;
    //[super dealloc];
}

@end
